package com.djl.zenithbite.interceptor;

import com.djl.zenithbite.common.threadlocal.UserContextHolder;
import com.djl.zenithbite.config.annotation.UserAuthAnnotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class UserAuthAspect {

    @Around("@annotation(userAuthAnnotation)")
    public Object checkUserAuthentication(ProceedingJoinPoint joinPoint, UserAuthAnnotation userAuthAnnotation) throws Throwable {
        // 在这里实现用户登录验证逻辑
        boolean isAuthenticated = checkIfUserIsAuthenticated();

        if (!isAuthenticated) {
            throw new SecurityException("User is not authenticated");
        }

        // 如果用户已登录，继续执行目标方法
        return joinPoint.proceed();
    }

    private boolean checkIfUserIsAuthenticated() {
        return UserContextHolder.getUser() != null;
    }
}